﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.business.dienstplan;
using personalplaner.business.stamm;
using personalplaner.common;
using personalplaner.dataprovider;
using System.Windows.Media;
using personalplaner.business.extensions;
using personalplaner.business.enums;

namespace personalplaner.servicemodel.dataaccess
{
	public interface IPlanDataAccess
	{
		#region Get Methods

		/// <summary>
		/// Returns a list with the active Plannames
		///    - GetActivePlannames(string mandid, bool addEmptyRow)
		/// </summary>
		/// <param name="onlyactive"></param>
		/// <returns></returns>
		IEnumerable<ArbeitsPlanHeader> GetArbeitsPlaene(bool onlyactive = false);

		/// <summary>
		/// Returns the specified arbeitsplan
		///    - GetActivePlannames(string mandid, bool addEmptyRow)
		/// </summary>
		/// <param name="planid"></param>
		/// <returns></returns>
		ArbeitsPlanHeader GetArbeitsPlan(int planid);

        /// <summary>
        /// Creates a list of every person in a plan. 
        /// prsID,name,nachname,abteilungid,abteilungname,prsschemaid,von,bis,rgb
        /// </summary>
        /// <param name="planID">PlanID Attribut</param>
        /// <returns>prsID,name,nachname,abteilungid,abteilungname,prsschemaid,von,bis,rgb,pensum</returns>
        IEnumerable<DienstPlanRowHeader> GetPlanRowHeaders(int planID);

        /// <summary>
        /// Creates a DataView with specific Data of a row. datum,name,dienstid,stunden,rgb
        /// </summary>
        /// <param name="planID">PlanID</param>
        /// <param name="prsSchemaID">PrsSchemaID</param>
        /// <param name="prsid"></param>
        /// <returns>datum,name,dienstid,stunden,rgb</returns>
        IEnumerable<PlanItem> GetPlanRow(int planID, int prsSchemaID, int prsid);

		string GetPlanName(int planId);

		/// <summary>
		/// [vortrag,soll,haben,ferien,anpassung]
		/// </summary>
		/// <param name="prsid"></param>
		/// <param name="abteilungid"></param>
		/// <param name="planid"></param>
		/// <returns></returns>
		TimeSaldo GetSaldo(int prsid, int abteilungid, int planid);

		int GetSchemaId(int planid);

		int GetPlanId(int schemaid);

        /// <summary>
        /// [von]
        /// </summary>
        /// <param name="planId">planid</param>
        /// <returns>[von]</returns>
        IEnumerable<Feiertag> GetFeiertageInPlan(int planId);

        string GetSchemaName(int schemaid);

		/// <summary>
		/// Checks if one of the abteilungen form the schema is already contained in a plan at the given date (month and year)
		///		- static public bool CheckIfPlanExists(int monatID, int schemaID, int jahrID)
		/// </summary>
		/// <param name="atId"></param>
		/// <param name="schemaId"></param>
		/// <returns></returns>
		bool DoesPlanExist(int atId, int schemaId);

		/// <summary>
		/// Gets a list of <see cref="SaldoAnpassung"/> items for a certain person
		/// </summary>
		/// <param name="prsid">prsid</param>
		/// <param name="abteilungid">abteilungid</param>
		/// <param name="planid">planid</param>
		/// <returns>[saldoid,datum,grund,stunden]</returns>
		IEnumerable<SaldoAnpassung> GetSaldoAnpassungen(int prsid, int abteilungid, int planid);

        /// <summary>
        /// Gets a list of items containing information about each plan in the year
        /// </summary>
        /// <param name="prsid"></param>
        /// <param name="yearid"></param>
        /// <returns></returns>
        IEnumerable<PlanOverviewHeader> GetPlanOverviewHeaders(int prsid, int yearid);

		///// <summary>
		///// gets a list of items containing information about each planed item in a plan
		///// </summary>
		///// <param name="settings"></param>
		///// <param name="prsid"></param>
		///// <param name="abteilungid"></param>
		///// <param name="planid"></param>
		///// <param name="dienstids"></param>
		///// <param name="dates"></param>
		///// <param name="all"></param>
		///// <returns></returns>
		//IEnumerable<PlanOverviewItem> GetPlanOverviewRow(DatabaseSettings settings, int prsid, int abteilungid, int planid, int[] dienstids, DateTime[] dates, bool all);

		/// <summary>
		/// Creates a list of shortdienst items containig all holidays in a year
		/// </summary>
		/// <param name="prsid"></param>
		/// <param name="atid"></param>
		/// <param name="yearid"></param>
		/// <returns></returns>
		IEnumerable<ShortDienst> GetPlanHolidays(int prsid, int atid, int yearid);

		#endregion

        #region Set Methods

        /// <summary>
        /// Saves a dienst in a specific plan on a specific day
        /// </summary>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        /// <param name="date"></param>
        /// <param name="dienstId"></param>
        bool SaveDienstInPlan(int planId, int prsId, int abteilungId, DateTime date, int dienstId);

        /// <summary>
        /// recalculates the saldos in a plan
        /// </summary>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        bool RecalculateSaldosInPlan(int planId, int prsId, int abteilungId);

        /// <summary>
        /// Saves a color in a specific plan on a specific day
        /// </summary>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        /// <param name="date"></param>
		/// <param name="color"></param>
        bool SaveColorInPlan(int planId, int prsId, int abteilungId, DateTime date, Color? color);

		/// <summary>
		/// Initializes all Dienstserien in Database for a plan
		/// </summary>
		/// <param name="planId">planid</param>
		void InitializeDienstSerie(int planId);

		/// <summary>
		/// Creates a new ArbeitsPlan [planid]
		/// </summary>
		/// <param name="planname">planname</param>
		/// <param name="arbeitstageid">atid</param>
		/// <param name="schemaid">schemaid</param>
		/// <param name="continuous">Soll das Schema fortlaufend erstellt werden?</param>
		/// <returns>[planid]</returns>
		int CreateNewPlan(string planname, int arbeitstageid, int schemaid, bool continuous);

		/// <summary>
		/// sets a new saldoanpassung to a person and returns the saldo id
		/// </summary>
		/// <param name="anpassung"></param>
		/// <returns></returns>
		int SetSaldoAnpassung(SaldoAnpassung anpassung);

        #endregion

		#region Update Methods

		/// <summary>
		/// Updates the Status of a plan
		/// </summary>
		/// <param name="planId">PlanID</param>
		/// <param name="status">Status</param>
		bool UpdatePlanStatus(int planId, enStatus status);

		/// <summary>
		/// Updates the name of the plan
		/// </summary>
		/// <param name="planId"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		bool UpdatePlanName(int planId, string name);

		#endregion

		#region Delete Methods

		/// <summary>
		/// Deletes the plan
		/// </summary>
		/// <param name="planId"></param>
		/// <returns></returns>
		bool DeletePlan(int planId);

		bool DeleteSaldoAnpassung(SaldoAnpassung anpassung);

		bool DeleteSaldoAnpassung(int saldoid);

		#endregion
	}

	public class PlanDataAccess : BaseDataAccess, IPlanDataAccess
	{
		public PlanDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

		#region Get Methods

		/// <summary>
		/// Returns a list with the active Plannames
		///    - GetActivePlannames(string mandid, bool addEmptyRow)
		/// </summary>
		/// <param name="onlyactive"></param>
		/// <returns></returns>
		public IEnumerable<ArbeitsPlanHeader> GetArbeitsPlaene(bool onlyactive = false)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetArbeitsPlaene(Settings.DatabaseSettings, /*Settings.Mandant,*/ onlyactive);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		/// <summary>
		/// Returns the specified arbeitsplan
		///    - GetActivePlannames(string mandid, bool addEmptyRow)
		/// </summary>
		/// <param name="planid"></param>
		/// <returns></returns>
		public ArbeitsPlanHeader GetArbeitsPlan(int planid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetArbeitsPlan(Settings.DatabaseSettings, planid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		/// <summary>
        /// Creates a list of every person in a plan. 
        /// prsID,name,nachname,abteilungid,abteilungname,prsschemaid,von,bis,rgb
        /// </summary>
        /// <param name="planID">PlanID Attribut</param>
        /// <returns>prsID,name,nachname,abteilungid,abteilungname,prsschemaid,von,bis,rgb,pensum</returns>
        public IEnumerable<DienstPlanRowHeader> GetPlanRowHeaders(int planID)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.GetPlanRowHeaders(Settings.DatabaseSettings, planID);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

        /// <summary>
        /// Creates a DataView with specific Data of a row. datum,name,dienstid,stunden,rgb
        /// </summary>
        /// <param name="planID">PlanID</param>
        /// <param name="prsSchemaID">PrsSchemaID</param>
        /// <param name="prsid"></param>
        /// <returns>datum,name,dienstid,stunden,rgb</returns>
        public IEnumerable<PlanItem> GetPlanRow(int planID, int prsSchemaID, int prsid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.GetPlanRow(Settings.DatabaseSettings, planID, prsSchemaID, prsid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

		public string GetPlanName(int planId)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetPlanName(Settings.DatabaseSettings, planId);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return "";
			}
		}

		/// <summary>
		/// [vortrag,soll,haben,ferien,anpassung]
		/// </summary>
		/// <param name="prsid"></param>
		/// <param name="abteilungid"></param>
		/// <param name="planid"></param>
		/// <returns></returns>
		public TimeSaldo GetSaldo(int prsid, int abteilungid, int planid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetSaldo(Settings.DatabaseSettings, prsid,abteilungid, planid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		public int GetSchemaId(int planid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetSchemaId(Settings.DatabaseSettings, planid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		public int GetPlanId(int schemaid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetPlanId(Settings.DatabaseSettings, schemaid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		/// <summary>
        /// [von]
        /// </summary>
        /// <param name="planId">planid</param>
        /// <returns>[von]</returns>
        public IEnumerable<Feiertag> GetFeiertageInPlan(int planId)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.GetFeiertageInPlan(Settings.DatabaseSettings, planId);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

        public string GetSchemaName(int schemaid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.GetSchemaName(Settings.DatabaseSettings, schemaid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return string.Empty;
            }  
        }

		/// <summary>
		/// Checks if one of the abteilungen form the schema is already contained in a plan at the given date (month and year)
		///		- static public bool CheckIfPlanExists(int monatID, int schemaID, int jahrID)
		/// </summary>
		/// <param name="atId"></param>
		/// <param name="schemaId"></param>
		/// <returns></returns>
		public bool DoesPlanExist(int atId, int schemaId)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.DoesPlanExist(Settings.DatabaseSettings, atId, schemaId, Settings.YearId);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}  
		}

		/// <summary>
		/// Gets a list of <see cref="SaldoAnpassung"/> items for a certain person
		/// </summary>
		/// <param name="prsid">prsid</param>
		/// <param name="abteilungid">abteilungid</param>
		/// <param name="planid">planid</param>
		/// <returns>[saldoid,datum,grund,stunden]</returns>
		public IEnumerable<SaldoAnpassung> GetSaldoAnpassungen(int prsid, int abteilungid, int planid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetSaldoAnpassungen(Settings.DatabaseSettings, prsid, abteilungid, planid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			} 
		}

        /// <summary>
        /// Gets a list of items containing information about each plan in the year
        /// </summary>
        /// <param name="prsid"></param>
        /// <param name="yearid"></param>
        /// <returns></returns>
        public IEnumerable<PlanOverviewHeader> GetPlanOverviewHeaders(int prsid, int yearid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.GetPlanOverviewHeaders(Settings.DatabaseSettings, prsid, yearid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            } 
        }

		///// <summary>
		///// gets a list of items containing information about each planed item in a plan
		///// </summary>
		///// <param name="settings"></param>
		///// <param name="prsid"></param>
		///// <param name="abteilungid"></param>
		///// <param name="planid"></param>
		///// <param name="dienstids"></param>
		///// <param name="dates"></param>
		///// <param name="all"></param>
		///// <returns></returns>
		//public IEnumerable<PlanOverviewItem> GetPlanOverviewRow(DatabaseSettings settings, int prsid, int abteilungid, int planid, int[] dienstids, DateTime[] dates, bool all)
		//{
		//    using (new WaitCursor())
		//    {
		//        try
		//        {
		//            return DataServices.PlanService.GetPlanOverviewRow(settings, prsid, abteilungid, planid, dienstids, dates, all);
		//        }
		//        catch (Exception e)
		//        {
		//            //TODO: show a messagebox here with the error
		//            Console.Write(e);
		//        }

		//        return null;
		//    } 
		//}

		/// <summary>
		/// Creates a list of shortdienst items containig all holidays in a year
		/// </summary>
		/// <param name="prsid"></param>
		/// <param name="atid"></param>
		/// <param name="yearid"></param>
		/// <returns></returns>
		public IEnumerable<ShortDienst> GetPlanHolidays(int prsid, int atid, int yearid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.GetPlanHolidays(Settings.DatabaseSettings, prsid, atid, yearid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			} 
		}

		#endregion

        #region Set Methods

        /// <summary>
        /// Saves a dienst in a specific plan on a specific day
        /// </summary>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        /// <param name="date"></param>
        /// <param name="dienstId"></param>
        public bool SaveDienstInPlan(int planId, int prsId, int abteilungId, DateTime date, int dienstId)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.SaveDienstInPlan(Settings.DatabaseSettings, planId, prsId, abteilungId, date, dienstId);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

        /// <summary>
        /// recalculates the saldos in a plan
        /// </summary>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        public bool RecalculateSaldosInPlan(int planId, int prsId, int abteilungId)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.PlanService.RecalculateSaldosInPlan(Settings.DatabaseSettings, planId, prsId, abteilungId);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

        /// <summary>
        /// Saves a color in a specific plan on a specific day
        /// </summary>
        /// <param name="planId"></param>
        /// <param name="prsId"></param>
        /// <param name="abteilungId"></param>
        /// <param name="date"></param>
		/// <param name="color"></param>
        public bool SaveColorInPlan(int planId, int prsId, int abteilungId, DateTime date, Color? color)
        {
            using (new WaitCursor())
            {
                try
                {
                    int argb = -1;
                    if (color != null)
                        argb = color.ToInt();
                    return DataServices.PlanService.SaveColorInPlan(Settings.DatabaseSettings, planId, prsId, abteilungId, date, argb);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

		/// <summary>
		/// Initializes all Dienstserien in Database for a plan
		/// </summary>
		/// <param name="planId">planid</param>
		public void InitializeDienstSerie(int planId)
		{
			using (new WaitCursor())
			{
				try
				{
					DataServices.PlanService.InitializeDienstSerie(Settings.DatabaseSettings, planId);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}
			}
		}

		/// <summary>
		/// Creates a new ArbeitsPlan [planid]
		/// </summary>
		/// <param name="planname">planname</param>
		/// <param name="arbeitstageid">atid</param>
		/// <param name="schemaid">schemaid</param>
		/// <param name="continuous">Soll das Schema fortlaufend erstellt werden?</param>
		/// <returns>[planid]</returns>
		public int CreateNewPlan(string planname, int arbeitstageid, int schemaid, bool continuous)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.CreateNewPlan(Settings.DatabaseSettings, planname, arbeitstageid, schemaid, continuous);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		/// <summary>
		/// sets a new saldoanpassung to a person and returns the saldo id
		/// </summary>
		/// <param name="anpassung"></param>
		/// <returns></returns>
		public int SetSaldoAnpassung(SaldoAnpassung anpassung)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.SetSaldoAnpassung(Settings.DatabaseSettings, anpassung);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

        #endregion

		#region Update Methods

		/// <summary>
		/// Updates the Status of a plan
		/// </summary>
		/// <param name="planId">PlanID</param>
		/// <param name="status">Status</param>
		public bool UpdatePlanStatus(int planId, enStatus status)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.UpdatePlanStatus(Settings.DatabaseSettings, planId, status);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		/// <summary>
		/// Updates the name of the plan
		/// </summary>
		/// <param name="planId"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public bool UpdatePlanName(int planId, string name)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.UpdatePlanName(Settings.DatabaseSettings, planId, name);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		#endregion

		#region Delete Methods

		/// <summary>
		/// Deletes the plan
		/// </summary>
		/// <param name="planId"></param>
		/// <returns></returns>
		public bool DeletePlan(int planId)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.DeletePlan(Settings.DatabaseSettings, planId);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		public bool DeleteSaldoAnpassung(SaldoAnpassung anpassung)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.DeleteSaldoAnpassung(Settings.DatabaseSettings, anpassung);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		public bool DeleteSaldoAnpassung(int saldoid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.PlanService.DeleteSaldoAnpassung(Settings.DatabaseSettings, saldoid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		#endregion

	}
}
